package com.example.shopping.controller;

import com.example.shopping.entity.Order;
import com.example.shopping.entity.User;
import com.example.shopping.entity.dto.OrderDTO;
import com.example.shopping.service.OrderService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;



    @GetMapping("/list")
    public String listOrders(Model model) {
        List<OrderDTO> orders = orderService.findAllOrdersWithUserInfoAndGoodsInfo();

        model.addAttribute("orders", orders);
        return "orderList"; // 对应 templates/orderList.html
    }




    // ✅ 处理 GET 请求（页面首次打开、分页等）
    @GetMapping("/query")
    public String orderQueryGet(Model model) {
        model.addAttribute("totalPages", 1);
        return "orderQuery";
    }

    @PostMapping("/query")
    public String orderQueryPost(Model model,
                                @RequestParam(value = "username", required = false, defaultValue = "") String username,
                                @RequestParam(value = "goodsname", required = false, defaultValue = "") String goodsname,
                                @RequestParam(value = "mailingaddress", required = false, defaultValue = "") String mailingaddress,
                                @RequestParam(value = "rphone", required = false, defaultValue = "") String rphone,
                                @RequestParam(value = "receiver", required = false, defaultValue = "") String receiver,
                                @RequestParam(value = "buytimebegin", required = false, defaultValue = "") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDate buytimebegin,
                                @RequestParam(value = "buytimeend", required = false, defaultValue = "") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDate buytimeend,
                                @RequestParam(value = "pageno", required = false, defaultValue = "1") int pageNo) {

        int pageSize = 5; // 每页显示的记录数
        Page<OrderDTO> page = orderService.queryOrders(username, goodsname,mailingaddress,rphone, receiver,buytimebegin,buytimeend, pageNo, pageSize);

        model.addAttribute("orders", page.getContent());
        model.addAttribute("currentPage", pageNo);
        Integer totalPages= page.getTotalPages();
        model.addAttribute("totalPages", totalPages == null ? 1 : totalPages);
        model.addAttribute("totalItems", page.getTotalElements());

        // 回显参数用于表单回填
        model.addAttribute("username", username);
        model.addAttribute("goodsname", goodsname);
        model.addAttribute("mailingaddress", mailingaddress);
        model.addAttribute("rphone", rphone);
        model.addAttribute("receiver", receiver);
        model.addAttribute("buytimebegin", buytimebegin);
        model.addAttribute("buytimeend", buytimeend);

        return "orderQuery";
    }


    @GetMapping("/ownList")
    public String listOwnOrders(Model model, HttpServletRequest request) {
        // 获取 session 对象
        HttpSession session = request.getSession();

        // 获取 session 中的属性
        User user =(User) session.getAttribute("loginUser");
        int uid = user.getUid();
        List<OrderDTO> orders = orderService.findOrdersByUid(uid);
        model.addAttribute("uid",uid);

        model.addAttribute("orders", orders);
        return "ownOrderList"; // 对应 templates/orderList.html
    }







}
